package com.ai5suoai.ams.web.ctrl.v1;

import com.ai5suoai.ams.entity.TestBean;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 系统的controller
 *
 * @author chenzhenjia
 * @since 2016/3/17
 */
@RestController
@RequestMapping("api/v1/sys/")
public class SystemCtrl {

    @RequestMapping(value = "ping", method = RequestMethod.GET)
    public ResponseEntity ping(final HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>(0) {{
            put("addr", request.getRemoteAddr());
            put("host", request.getRemoteHost());
            put("id", request.getSession().getId());
        }};
        return ResponseEntity.ok(map);
    }

    @RequestMapping(value = "v")
    public ResponseEntity v(@Valid TestBean bean) {
        return ResponseEntity.ok(bean);
    }

    /**
     * 401 状态码用的地址
     */
    @RequestMapping("401")
    public ResponseEntity unauthorized() {
        Map<String, String> map = new HashMap<>(1);
        map.put("msg", "授权失败");
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(map);
    }

    @RequestMapping("demo500")
    public ResponseEntity demo500() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "服务器出错");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
    }
}
